package com.medical.data.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.medical.data.domain.FormTemplate;
import com.medical.data.domain.ProjectTemplate;
import com.medical.data.exception.MyException;
import com.medical.data.mapper.ProjectTemplateMapper;
import com.medical.data.service.ProjectTemplateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medical.data.service.UserPermissionService;
import com.medical.data.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cj
 * @since 2024-02-17
 */
@Service
public class ProjectTemplateServiceImpl extends ServiceImpl<ProjectTemplateMapper, ProjectTemplate> implements ProjectTemplateService {
    @Autowired
    private UserPermissionService userPermissionService;

    @Override
    public List<FormTemplate> getNodesByProjectId(String projectId) {
        userPermissionService.getFormPermission(projectId);
        List<FormTemplate> formTemplates = baseMapper.getNodesByProjectId(projectId);
        if (CollectionUtil.isEmpty(formTemplates))
            throw new MyException(ResultCode.ERROR.getCode(), "请用户创建节点");
        return formTemplates;
    }

    @Override
    public List<FormTemplate> getFollowNodesByProjectId(String projectId) {
        userPermissionService.getFollowUpPermission(projectId);
        List<FormTemplate> formTemplates = baseMapper.getFollowNodesByProjectId(projectId);
//        if (CollectionUtil.isEmpty(formTemplates))
//            throw new MyException(ResultCode.ERROR.getCode(), "请用户创建节点");
        return formTemplates;
    }

    @Override
    public Boolean isExitTemplate(String projectId) {
        List<ProjectTemplate> projectTemplates = baseMapper.selectList(Wrappers.<ProjectTemplate>lambdaQuery().eq(ProjectTemplate::getProjectId, projectId).select(ProjectTemplate::getId));
        if (!CollectionUtil.isEmpty(projectTemplates))
            return true;
        return false;

    }

    @Override
    public List<FormTemplate> getAllNodes(String projectId) {
        List<FormTemplate> formTemplates = baseMapper.getAllNodesByProjectId(projectId);
        if (CollectionUtil.isEmpty(formTemplates))
            throw new MyException(ResultCode.ERROR.getCode(), "请用户创建节点");
        return formTemplates;
    }

    @Override
    public void deleteNodeById(String nodeId) {
        LambdaQueryWrapper<ProjectTemplate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProjectTemplate::getFormTemplateId, nodeId);
        int delete = this.baseMapper.delete(wrapper);
        if (delete < 1) {
            throw new MyException(ResultCode.ERROR.getCode(), "删除项目节点失败");
        }
    }

    @Override
    public String getProjectIdByNodeId(String nodeId) {
        LambdaQueryWrapper<ProjectTemplate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProjectTemplate::getFormTemplateId, nodeId);
        return this.baseMapper.selectOne(wrapper).getProjectId();
    }

}
